package com.imis.module.system.bus;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imis.base.constant.CommonConstant;
import com.imis.base.constant.enums.SysTipEnum;
import com.imis.base.globle.Result;
import com.imis.base.util.ConvertUtils;
import com.imis.base.util.CurrentUserUtils;
import com.imis.module.base.BaseBus;
import com.imis.module.system.model.converter.SysRoleConverter;
import com.imis.module.system.model.po.SysMenu;
import com.imis.module.system.model.po.SysRole;
import com.imis.module.system.model.po.SysUser;
import com.imis.module.system.model.ro.*;
import com.imis.module.system.model.vo.SysRoleVO;
import com.imis.module.system.model.vo.SysUserVO;
import com.imis.module.system.service.ISysMenuService;
import com.imis.module.system.service.ISysRoleService;
import com.imis.module.system.service.ISysUserRoleService;
import com.imis.module.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统角色表-菜单权限组 业务处理类
 * </p>
 *
 * @author XinLau
 * @since 2020-03-12
 */
@Service
public class SysRoleBus extends BaseBus {

    /**
     * 系统角色表-菜单权限组 服务类
     */
    private ISysRoleService serviceBySysRoleService;

    @Autowired
    public void setServiceBySysRoleService(ISysRoleService serviceBySysRoleService) {
        this.serviceBySysRoleService = serviceBySysRoleService;
    }

    /**
     * 功能菜单表 服务类
     */
    private ISysMenuService serviceBySysMenuService;

    @Autowired
    public void setServiceBySysMenuService(ISysMenuService serviceBySysMenuService) {
        this.serviceBySysMenuService = serviceBySysMenuService;
    }

    /**
     * 用户角色关联表 服务类
     */
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    public void setiSysUserRoleService(ISysUserRoleService sysUserRoleService) {
        this.sysUserRoleService = sysUserRoleService;
    }

    /**
     * 用户信息 服务类
     */
    private ISysUserService sysUserService;

    @Autowired
    public void setSysUserService(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 添加角色校验
     *
     * @param sysRoleAddRO - 角色
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result roleAddVerification(final SysRoleAddRO sysRoleAddRO) {
        Result result = new Result<>();
        SysRole sysRole = serviceBySysRoleService.queryRoleByName(sysRoleAddRO.getRoleName());
        if (sysRole != null) {
//            根据角色信息查询，该角色存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_ADD_ERR_NAME_REPEAT.toString(), sysRole.getRoleName());
            return result.errorFormat(SysTipEnum.ROLE_ADD_ERR_NAME_REPEAT, sysRole.getRoleName());
        }
        sysRole = serviceBySysRoleService.queryRoleByCode(sysRoleAddRO.getRoleCode());
        if (sysRole != null) {
//            根据角色编码查询，该角色编码存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_ADD_ERR_CODE_REPEAT.toString(), sysRole.getRoleCode());
            return result.errorFormat(SysTipEnum.ROLE_ADD_ERR_CODE_REPEAT, sysRole.getRoleCode());
        }
        result.setResult(SysRoleConverter.INSTANCE.addRo2Vo(sysRoleAddRO));
        return result;
    }

    /**
     * 删除角色校验
     *
     * @param sysRole - 角色
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result roleDeleteVerification(final SysRole sysRole) {
        Result result = new Result<>();
        if (ConvertUtils.isEmpty(sysRole)) {
//            1.根据角色信息查询，该角色不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_DELETE_ERR_NON.toString());
            return result.errorFormat(SysTipEnum.ROLE_DELETE_ERR_NON);
        }
//        判断角色是否分配给用户
        List<Long> sysUserIdList = serviceBySysRoleService.selectSysUserIdArrayByRoleId(sysRole.getId());
        if (sysUserIdList.size() > 0) {
//            2.根据角色信息查询，该角色已分配给用户使用
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_DELETE_ERR_ASSIGNED.toString(), sysRole.getRoleName(), JSONUtil.toJsonStr(sysUserIdList));
            return result.errorFormat(SysTipEnum.ROLE_DELETE_ERR_ASSIGNED, sysRole.getRoleName(), JSONUtil.toJsonStr(sysUserIdList));
        }
        result.setResult(SysRoleConverter.INSTANCE.domain2vo(sysRole));
        return result;
    }

    /**
     * 更新角色校验
     *
     * @param sysRoleUpdateRO - 角色
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    private Result roleUpdateVerification(final SysRoleUpdateRO sysRoleUpdateRO) {
        SysRole sysRole = serviceBySysRoleService.getById(sysRoleUpdateRO.getId());
        Result result = new Result<>();
        if (ConvertUtils.isEmpty(sysRole)) {
//            1.根据角色信息查询，该角色不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_UPDATE, SysTipEnum.ROLE_UPDATE_ERR_NON.toString(), sysRoleUpdateRO.getRoleName());
            return result.errorFormat(SysTipEnum.ROLE_UPDATE_ERR_NON, sysRoleUpdateRO.getRoleName());
        }
//        2.校验角色名重复
        if (!sysRole.getRoleName().equals(sysRoleUpdateRO.getRoleName())) {
//            前端传递角色名与数据库查询出来的不一致做角色名重复校验（角色名修改做校验）
            SysRole sysRoleByName = serviceBySysRoleService.queryRoleByName(sysRoleUpdateRO.getRoleName());
            if (sysRoleByName != null) {
//                根据角色名查询，该角色名存在
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_UPDATE_ERR_NAME_REPEAT.toString(), sysRoleUpdateRO.getRoleName());
                return result.errorFormat(SysTipEnum.ROLE_UPDATE_ERR_NAME_REPEAT, sysRoleUpdateRO.getRoleName());
            }
        }
//        3.角色编码重复校验
        if (!sysRole.getRoleCode().equals(sysRoleUpdateRO.getRoleCode())) {
            SysRole sysRoleByCode = serviceBySysRoleService.queryRoleByCode(sysRoleUpdateRO.getRoleCode());
            if (sysRoleByCode != null) {
//                根据角色编码查询，该角色编码存在
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_UPDATE_ERR_CODE_REPEAT.toString(), sysRoleUpdateRO.getRoleCode());
                return result.errorFormat(SysTipEnum.ROLE_UPDATE_ERR_CODE_REPEAT, sysRoleUpdateRO.getRoleCode());
            }
        }
        result.setResult(SysRoleConverter.INSTANCE.domain2vo(sysRole));
        return result;
    }

    /**
     * 为角色授权功能菜单权限检验
     *
     * @param sysGrantMenuPermissionToRoleRO - 角色授权功能菜单
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/21 11:05
     */
    private Result grantMenuPermissionsToRoleVerification(final SysGrantMenuPermissionToRoleRO sysGrantMenuPermissionToRoleRO) {
//        1.验证角色合法性
        Long roleId = sysGrantMenuPermissionToRoleRO.getRoleId();
        SysRole sysRole = serviceBySysRoleService.getById(roleId);
        if (ConvertUtils.isEmpty(sysRole)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_MENU_TO_ROLE_ERR_NON_ROLE.toString(), roleId);
            return Result.errorsFormat(SysTipEnum.GRANT_MENU_TO_ROLE_ERR_NON_ROLE, roleId);
        }
//        2.验证功能菜单的合法性
        List<Long> menuIdList = sysGrantMenuPermissionToRoleRO.getMenuIdList();
        List<SysMenu> sysMenuList = serviceBySysMenuService.listByIds(menuIdList);
        if (menuIdList.size() != sysMenuList.size()) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_MENU_TO_ROLE_ERR_NON_MENU.toString(), menuIdList.toString());
            return Result.errorsFormat(SysTipEnum.GRANT_MENU_TO_ROLE_ERR_NON_MENU, menuIdList.toString());
        }
        return Result.ok();
    }

    /**
     * 角色授权用户校验
     *
     * @param sysGrantRoleToUsersRO - 角色授权用户
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/20 14:18
     */
    private Result grantRoleToUsersVerification(final SysGrantRoleToUsersRO sysGrantRoleToUsersRO) {
        // 1.验证角色合法性
        Long roleId = sysGrantRoleToUsersRO.getRoleId();
        SysRole sysRole = serviceBySysRoleService.getById(roleId);
        if (ConvertUtils.isEmpty(sysRole)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR_NON_ROLE.toString(), roleId);
            return Result.errorsFormat(SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR_NON_ROLE, roleId);
        }
        // 2.验证用户合法性
        List<Long> userIdList = sysGrantRoleToUsersRO.getUserIdList();
        List<SysUser> sysUserList = sysUserService.listByIds(userIdList);
        if (sysUserList.size() != userIdList.size()) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR_NON_USER.toString(), userIdList.toString());
            return Result.errorsFormat(SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR_NON_USER, userIdList.toString());
        }
        return Result.ok();
    }

    /**
     * 取消角色授权用户校验
     *
     * @param sysUnGrantRoleToUsersRO - 取消角色授权用户
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/20 14:18
     */
    private Result unGrantRoleToUsersVerification(final SysUnGrantRoleToUsersRO sysUnGrantRoleToUsersRO) {
        // 1.验证角色合法性
        Long roleId = sysUnGrantRoleToUsersRO.getRoleId();
        SysRole sysRole = serviceBySysRoleService.getById(roleId);
        if (ConvertUtils.isEmpty(sysRole)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR_NON_ROLE.toString(), roleId);
            return Result.errorsFormat(SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR_NON_ROLE, roleId);
        }
        // 2.验证用户合法性
        List<Long> userIdList = sysUnGrantRoleToUsersRO.getUserIdList();
        List<SysUser> sysUserList = sysUserService.listByIds(userIdList);
        if (sysUserList.size() != userIdList.size()) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR_NON_USER.toString(), userIdList.toString());
            return Result.errorsFormat(SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR_NON_USER, userIdList.toString());
        }
        return Result.ok();
    }

    /**
     * 分页查询角色信息
     *
     * @param pagingQueryRoleDTO - 角色信息查询对象
     * @return Result
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/9 9:47
     */
    public Result pagingQuerySysRoleListByParameter(final PagingQueryRoleDTO pagingQueryRoleDTO) {
        try {
            Page<SysRoleVO> sysRoleSelectROPage = serviceBySysRoleService.pagingQuerySysRoleListByParameter(pagingQueryRoleDTO);
            return Result.ok(sysRoleSelectROPage);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.ROLE_PAGE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.ROLE_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 创建角色
     *
     * @param sysRoleAddRO - 角色添加对象
     * @return Result - 角色对象
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result addRole(SysRoleAddRO sysRoleAddRO) {
//        1.校验角色重复
        Result result = roleAddVerification(sysRoleAddRO);
        if (!result.isSuccess()) {
            return result;
        }
//        2.创建新角色
        SysRole sysRole = SysRoleConverter.INSTANCE.addRo2domain(sysRoleAddRO);
        sysRole.setCreateBy(CurrentUserUtils.getLoginUserId());
        if (serviceBySysRoleService.saveRole(sysRole)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_ADD_OK.toString(), sysRole.getRoleName());
            result.setResult(SysRoleConverter.INSTANCE.domain2vo(sysRole));
            return result.success();
        }
//        添加失败
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_ADD_ERR.toString(), sysRoleAddRO.getRoleName());
        return result.errorFormat(SysTipEnum.ROLE_ADD_ERR, sysRoleAddRO.getRoleName());
    }

    /**
     * 删除角色
     *
     * @param roleIdentification - 角色标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result deleteRole(final Long roleIdentification) {
//        1.删除角色校验
        SysRole sysRole = serviceBySysRoleService.getById(roleIdentification);
        Result result = roleDeleteVerification(sysRole);
        if (!result.isSuccess()) {
            return result;
        }
//        2.清除角色分配的权限
        boolean cleanRolePermissionsById = serviceBySysRoleService.cleanRolePermissionsById(roleIdentification);
        if (!cleanRolePermissionsById) {
//            清除权限失败
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_DELETE_ERR_PERMISSIONS.toString(), sysRole.getRoleName());
            return result.errorFormat(SysTipEnum.ROLE_DELETE_ERR_PERMISSIONS, sysRole.getRoleName());
        }
//        3.删除角色
        boolean deleteRole = serviceBySysRoleService.deleteRole(roleIdentification);
        if (!deleteRole) {
//            删除角色失败
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_DELETE_ERR.toString(), sysRole.getRoleName());
            return result.errorFormat(SysTipEnum.ROLE_DELETE_ERR, sysRole.getRoleName());
        }
//        TODO:this 删除对应角色的权限缓存
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_DELETE_OK.toString(), sysRole.getRoleName());
        return Result.ok();
    }

    /**
     * 查看角色
     *
     * @param roleIdentification - 角色标识
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result queryRoleById(final Long roleIdentification) {
        SysRole sysRole = serviceBySysRoleService.getById(roleIdentification);
        if (ConvertUtils.isEmpty(sysRole)) {
//            1.根据角色信息查询，该角色不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_QUERY_ERR.toString());
            return Result.errorsFormat(SysTipEnum.ROLE_QUERY_ERR);
        }
        return Result.ok(SysRoleConverter.INSTANCE.domain2vo(sysRole));
    }

    /**
     * 根据角色标识查看用户
     *
     * @param pagingQueryUserByRoleIdDTO - 根据角色查询用户对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result pagingQuerySysUserListByRoleIdParameter(final PagingQueryUserByRoleIdDTO pagingQueryUserByRoleIdDTO) {
        SysRole sysRole = serviceBySysRoleService.getById(pagingQueryUserByRoleIdDTO.getRoleId());
        if (ConvertUtils.isEmpty(sysRole)) {
//            1.根据角色信息查询，该角色不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_QUERY_ERR.toString());
            return Result.errorsFormat(SysTipEnum.ROLE_QUERY_ERR);
        }
        Page<SysUserVO> sysUserList = sysUserRoleService.pagingQuerySysUserListByRoleIdParameter(pagingQueryUserByRoleIdDTO);
        return Result.ok(sysUserList);
    }

    /**
     * 根据角色标识查看未授权用户
     *
     * @param pagingQueryUserByRoleIdDTO - 根据角色查询用户对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result pagingQueryUnauthorizedSysUserListByRoleIdParameter(final PagingQueryUserByRoleIdDTO pagingQueryUserByRoleIdDTO) {
        SysRole sysRole = serviceBySysRoleService.getById(pagingQueryUserByRoleIdDTO.getRoleId());
        if (ConvertUtils.isEmpty(sysRole)) {
//            1.根据角色信息查询，该角色不存在
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.ROLE_QUERY_ERR.toString());
            return Result.errorsFormat(SysTipEnum.ROLE_QUERY_ERR);
        }
        Page<SysUserVO> sysUserList = sysUserRoleService.pagingQueryUnauthorizedSysUserListByRoleIdParameter(pagingQueryUserByRoleIdDTO);
        return Result.ok(sysUserList);
    }

    /**
     * 修改角色
     *
     * @param sysRoleUpdateRO - 角色修改对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/12 17:26
     */
    public Result updateRole(SysRoleUpdateRO sysRoleUpdateRO) {
//        1.修改角色校验
        Result result = roleUpdateVerification(sysRoleUpdateRO);
        if (!result.isSuccess()) {
            return result;
        }
//        2.角色信息修改
        boolean update = serviceBySysRoleService.updateByRoleIdentification(sysRoleUpdateRO);
        if (update) {
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.ROLE_UPDATE_ERR.toString(), sysRoleUpdateRO.getRoleName());
        return result.errorFormat(SysTipEnum.ROLE_UPDATE_ERR, sysRoleUpdateRO.getRoleName());
    }

    /**
     * 分页查找已授权角色
     *
     * @param sysUserRoleSelectRO - 授权角色查询对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/20 15:33
     */
    public Result pagingQueryGrantRoleListByParameter(final PagingQueryUserRoleDTO sysUserRoleSelectRO) {
        try {
            Page<SysRoleVO> sysRoleSelectROPage = serviceBySysRoleService.pagingQueryGrantRoleListByParameter(sysUserRoleSelectRO);
            return Result.ok(sysRoleSelectROPage);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.GRANT_ROLE_PAGE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.GRANT_ROLE_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 分页查询未授权角色
     *
     * @param sysUserRoleSelectRO - 授权角色查询对象
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/20 15:33
     */
    public Result pagingQueryNotGrantRoleListByParameter(final PagingQueryUserRoleDTO sysUserRoleSelectRO) {
        try {
            Page<SysRoleVO> sysRoleSelectROPage = serviceBySysRoleService.pagingQueryNotGrantRoleListByParameter(sysUserRoleSelectRO);
            return Result.ok(sysRoleSelectROPage);
        } catch (Exception e) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_QUERY, SysTipEnum.NOT_GRANT_ROLE_PAGE_QUERY_ERR.toString(), e.getMessage());
            return Result.errorsFormat(SysTipEnum.NOT_GRANT_ROLE_PAGE_QUERY_ERR, e.getMessage());
        }
    }

    /**
     * 为角色授权功能菜单权限
     *
     * @param sysGrantMenuPermissionToRoleRO - 角色授权功能菜单
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/21 10:59
     */
    public Result grantMenuPermissionsToRole(SysGrantMenuPermissionToRoleRO sysGrantMenuPermissionToRoleRO) {
//        1.为角色授权功能菜单权限检验
        Result result = grantMenuPermissionsToRoleVerification(sysGrantMenuPermissionToRoleRO);
        if (result.isSuccess()) {
//            2.授权操作
            boolean grant = serviceBySysRoleService.grantMenuPermissionsToRole(sysGrantMenuPermissionToRoleRO);
            if (grant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_MENU_TO_ROLE_OK.toString(), sysGrantMenuPermissionToRoleRO.getRoleId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.GRANT_MENU_TO_ROLE_ERR.toString(), sysGrantMenuPermissionToRoleRO.getRoleId());
        return Result.errorsFormat(SysTipEnum.GRANT_MENU_TO_ROLE_ERR, sysGrantMenuPermissionToRoleRO.getRoleId());
    }

    /**
     * 撤销角色功能菜单权限
     *
     * @param roleId - 角色编号
     * @return Result -
     * @author XinLau
     * @creed The only constant is change ! ! !
     * @since 2020/3/21 13:45
     */
    public Result terminateAllMenuPermissionsToRole(final Long roleId) {
//        1.验证角色合法性
        SysRole sysRole = serviceBySysRoleService.getById(roleId);
        if (ConvertUtils.isEmpty(sysRole)) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_MENU_TO_ROLE_ERR_NON_ROLE.toString(), roleId);
            return Result.errorsFormat(SysTipEnum.TERMINATE_ALL_MENU_TO_ROLE_ERR_NON_ROLE, roleId);
        }
//        2.撤销授权
        boolean terminate = serviceBySysRoleService.cleanRolePermissionsById(roleId);
        if (terminate) {
            addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_MENU_TO_ROLE_OK.toString(), sysRole.getRoleName());
            return Result.ok();
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_DELETE, SysTipEnum.TERMINATE_ALL_MENU_TO_ROLE_ERR.toString(), sysRole.getRoleName());
        return Result.errorsFormat(SysTipEnum.TERMINATE_ALL_MENU_TO_ROLE_ERR, sysRole.getRoleName());
    }

    /**
     * 角色授权用户
     *
     * @param sysGrantRoleToUsersRO - 角色授权用户
     * @return Result -
     * @creed The only constant is change ! ! !
     * @since 2020/7/3 13:45
     */
    public Result authorizingRoleToUsers(final SysGrantRoleToUsersRO sysGrantRoleToUsersRO) {
//        1.角色授权用户校验
        Result result = grantRoleToUsersVerification(sysGrantRoleToUsersRO);
        if (result.isSuccess()) {
//            2.授权操作
            boolean grant = sysUserRoleService.authorizingRoleToUsers(sysGrantRoleToUsersRO);
            if (grant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_ROLE_TO_USERS_OK.toString(), sysGrantRoleToUsersRO.getRoleId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR.toString(), sysGrantRoleToUsersRO.getRoleId());
        return Result.errorsFormat(SysTipEnum.AUTHORIZING_ROLE_TO_USERS_ERR, sysGrantRoleToUsersRO.getRoleId());
    }

    /**
     * 取消角色授权用户
     *
     * @param sysUnGrantRoleToUsersRO - 取消角色授权用户
     * @return Result -
     * @creed The only constant is change ! ! !
     * @since 2020/7/3 13:45
     */
    public Result unAuthorizingRoleToUsers(final SysUnGrantRoleToUsersRO sysUnGrantRoleToUsersRO) {
//        1.角色授权用户校验
        Result result = unGrantRoleToUsersVerification(sysUnGrantRoleToUsersRO);
        if (result.isSuccess()) {
//            2.授权操作
            boolean unGrant = sysUserRoleService.unAuthorizingRoleToUsers(sysUnGrantRoleToUsersRO);
            if (unGrant) {
                addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_OK.toString(), sysUnGrantRoleToUsersRO.getRoleId());
                return Result.ok();
            }
        }
        addLog(CommonConstant.LOG_TYPE_OPERATE, CommonConstant.OPERATE_TYPE_ADD, SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR.toString(), sysUnGrantRoleToUsersRO.getRoleId());
        return Result.errorsFormat(SysTipEnum.UN_AUTHORIZING_ROLE_TO_USERS_ERR, sysUnGrantRoleToUsersRO.getRoleId());
    }

}